package com.design.softwaredesign.controller;

import com.design.softwaredesign.front.DonationInfo;
import com.design.softwaredesign.front.GoodInfo;
import com.design.softwaredesign.service.DonationService;
import com.design.softwaredesign.service.GoodService;
import com.design.softwaredesign.utils.OssUtil;
import com.design.softwaredesign.vo.DataVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * @author :WaiterXiaoYY
 * @description: TODO
 * @data :2020/11/14 17:09
 */
@Api(description = "捐赠物品控制层")
@RestController
@RequestMapping("/web")
public class DonationController {
    @Autowired
    private OssUtil ossUtil;
    @Autowired
    private DonationService donationService;

    @GetMapping("/getdonationid")
    public DataVo getDonationId() {
        DataVo dataVo = new DataVo();
        List<String> id = new ArrayList<>();
        id.add(UUID.randomUUID().toString());
        dataVo.setMsg("商品id生成成功");
        dataVo.setCode(200);
        dataVo.setData(id);
        return dataVo;
    }

    @PostMapping("/donation/uploadImage")
    public DataVo uploadImage(@RequestParam("donationid") String donationId, @RequestParam("file") MultipartFile file) {
        DataVo dataVo = new DataVo();
        String url = "";
        try {
            url = ossUtil.checkImage(file);
            donationService.addImage(donationId, url);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return dataVo;
        }
    }

    @PostMapping("/addDonation")
    public DataVo addGood(@RequestBody DonationInfo donationInfo) {
        return donationService.addDonation(donationInfo);
    }

    @GetMapping("/getdonationlist")
    public DataVo getDonationList(Integer pageNum,  Integer pageSize, String query, String userId) {
        return donationService.getDonationList(pageNum, pageSize, query, userId);
    }

    @PostMapping("/deleteDonationById")
    @ApiOperation(value = "删除捐赠信息")
    public DataVo deleteDonationById(@RequestParam("donationId") String donationId) {
        return  donationService.deleteDonationById(donationId);
    }

    @GetMapping("/getdonationtimestamp")
    @ApiOperation(value = "获取时间线")
    public DataVo getDonationTimestamp(@RequestParam("donationId") String donationId) {
        return  donationService.getDonationTimestamp(donationId);
    }
}
